package com.mew.common.dc.redis;

import com.mew.common.config.RedisConstantValue;
import com.mew.common.entity.game.WinFree;
import com.mew.common.entity.user.Profile;
import com.mew.common.entity.user.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @Author K.Song
 * @Date 2020/11/6 0006
 **/
@Service
@Slf4j
public class UserProfileRedisService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    public User getRedisUserById(long userId){
        if (userId < 1){
            return null;
        }
        return (User) redisTemplate.opsForHash().get(RedisConstantValue.USER_INFO, userId);
    }


    public Profile getRedisProfileById(long userId){
        if (userId <1){
            return null;
        }
        return (Profile) redisTemplate.opsForHash().get(RedisConstantValue.USER_PROFILE, userId);
    }

    public Profile getRedisAgentById(long userId){
        if (userId <1){
            return null;
        }
        return (Profile) redisTemplate.opsForHash().get(RedisConstantValue.USER_AGENT, userId);
    }


    public WinFree getRedisWinfree(long userId, int roomId){
        if (userId <1 || roomId <1){
            return null;
        }
        return (WinFree)redisTemplate.opsForHash().get(RedisConstantValue.USER_WINFREE, String.format(RedisConstantValue.USER_WINFREE_TOPIC, userId, roomId));
    }

    public void updateRedisWinfree(WinFree winFree){
        redisTemplate.opsForHash().put(RedisConstantValue.USER_WINFREE, String.format(RedisConstantValue.USER_WINFREE_TOPIC, winFree.getUserId(), winFree.getRoomId()), winFree);
    }

}
